$(function(){
    // //首页头部用户块显示
	$('#userTop').on('mouseover',function(){
		$('.userdetail').show();
	}).on('mouseout',function(){
		$('.userdetail').hide();
	});
	$('.userdetail').on('mouseover',function(){
		$('.userdetail').show();
	}).on('mouseout',function(){
		$('.userdetail').hide();
	});

    //登录表单提交
    $('#jsLoginBtn').on('click',function(){
        login_form_submit();
    });
    //登录表单键盘事件
    $("#jsLoginForm").keydown(function(event){
        if(event.keyCode == 13) {
            $('#jsLoginBtn').trigger('click');
        }
    });
    //找回密码表单提交
    //找回密码邮箱验证

    //注册
    $('#jsEmailRegBtn').on('click',function(){
        register_form_submit()
    });

    //激活邮箱事件

    //重置错误信息
    $('input[type=text]').focus(function(){
       $('#jsLoginTips').text('');
       $('#jsEmailTips').text('');
       $('#jsFindPwdBtn').text('');
    })

    //顶部的搜索栏
    $('#search_btn').click(function(){
        search_click();
    });
     //刷新验证码
    $('.captcha').click(function(){
        refresh_captcha();
    });

});

//验证码
function refresh_captcha(){
    $.get('/captcha/refresh/?'+Math.random(),function(result){
        $('.captcha').attr('src',result.image_url);
        $('#id_captcha_0').attr('value',result.key);
    });
}

//搜索栏
function search_click(){
    var search=$('#search').val();
    if(search == ''){
        return;
    }
    $.ajax({
        cache: false,
        type:'GET',
        url:'/movie/search/?search='+search,
        async: true,
        success:function(data){
            window.location.href='/movie/search/?search='+search;
        }
    });
}

//注册
function register_form_submit(){
    var $jsEmailRegBtn=$('#jsEmailRegBtn'),
        $jsEmailTips=$('#jsEmailTips');
    // var  reg=/^\w+((\.\w+){0,3})@\w+(\.\w{2,3}){1,3}$/;
    // if(!reg.test($('#id_email').val()) && $('#id_email').val()!= ''){
    //     $jsEmailTips.text('邮箱格式不正确。');
    //     return;
    // }
    $.ajax({
        cache: false,
        type: 'post',
        dataType:'json',
        url:"/register/",
        data:$('#email_register_form').serialize(),
        async: true,
        beforeSend:function(){
            $jsEmailRegBtn.val('注册中...');
            $jsEmailRegBtn.attr("disabled","disabled");},
        success:function(data){
            if(data.status == 'success'){
                alert('请前往邮箱激活用户！');
                setTimeout(function(){
                    window.location.href=''; //刷新页面
                },5000);
            }else{
                if(data.email)
                    $jsEmailTips.text('邮箱'+data.email);
                else if (data.password)
                $jsEmailTips.text('密码'+data.password);
                else if(data.captcha)
                    $jsEmailTips.text(data.captcha);
                else if(data.msg)
                    $jsEmailTips.text('用户已存在');
           }
            },
        complete:function(XMLHttpRequest){
            $jsEmailRegBtn.val("注册并登录");
            $jsEmailRegBtn.removeAttr("disabled");
        }
    });
}

//登录表单提交
function login_form_submit(){
    var $jsLoginBtn = $('#jsLoginBtn'),
        $jsLoginTips = $('#jsLoginTips');
    $.ajax({
        cache: false,
        type: 'post',
        dataType:'json',
        url:"/login/",
        data:$('#jsLoginForm').serialize(),
        async: true,
        beforeSend:function(XMLHttpRequest){
            $jsLoginBtn.val("登录中...");
            $jsLoginBtn.attr("disabled","disabled");
        },
        success: function(data) {
            if(data.username)
                $jsLoginTips.text(data.username);
            else if(data.password)
                $jsLoginTips.text(data.password);
            else{
                if(data.status == "success"){
                    $('#jsLoginForm')[0].reset();//重置表单
                    window.location.href = window.location.href;
                }else if(data.status == "failure"){
                    //注册账户处于未激活状态
                    if(data.msg=='no_active'){
                        $jsLoginTips.text('账户未激活，请前往激活');
                    }else{
                        $jsLoginTips.text("账号或者密码错误，请重新输入");
                    }
                }
            }
        },
        complete: function(XMLHttpRequest){
            $jsLoginBtn.val("登录");
            $jsLoginBtn.removeAttr("disabled");
        }
    });
}
